/*
 * @Author: liuxinyea
 * @Date: 2024-08-11 10:16:35
 * @LastEditors: liuxinyea
 * @LastEditTime: 2024-08-11 21:25:25
 * @Description: 描述
 */
import { _decorator, Animation, Component, EventMouse, find, Input, input, instantiate, Node, Prefab, resources, Vec3 } from 'cc';
import { SingleInstanceWithCom } from '../base/SingleInstanceWithCom';
import { Plant } from '../Plant';
import { PlantType, PlantTypeMap } from '../Glabol';
import { Cell } from '../Cell';
const { ccclass, property } = _decorator;

@ccclass('MouseManager')
export class MouseManager extends Component {
    private currentPlant: Node;
    // 单例模式
    private static _instance: MouseManager;
    public static get instance(): MouseManager {
        return this._instance;
    }
    protected onLoad(): void {
        if (MouseManager._instance == null) {
            MouseManager._instance = this;
        } else {
            console.log("SunManager is already exist");
            this.node.destroy();
        }
        input.on(Input.EventType.MOUSE_MOVE,this.onMouseMove,this);
    }
    protected onDestroy(): void {
        input.off(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
    }
    onMouseMove(event: EventMouse){
        this.followCursor(event);
    }
    followCursor(event: EventMouse){
        const mousePosition = event.getUILocation();
        let worldPos = new Vec3(mousePosition.x, mousePosition.y, 0);
        if (this.currentPlant)
            this.currentPlant.worldPosition = worldPos;
    }
    async addPlant(platType: PlantType, event: EventMouse): Promise<boolean>{
        console.log(platType, PlantType.Sunflower);
        if (this.currentPlant) {
          return false
        }
        const plantPrefab =await this.getPlantPrefat(platType)
        if(!plantPrefab){
           console.log('要种植的植物预制体不存在');
           return false;
        }
        this.currentPlant = instantiate(plantPrefab) as any as Node;
        this.currentPlant.setParent(find('Canvas/Game'))
        this.followCursor(event)
        return true
    }
    async getPlantPrefat(plantType: PlantType):Promise<Prefab>{
        return new Promise((resolve,reject)=>{
            resources.loadDir('Prefab/Plant', (error, data) => {                
                if (error) {
                    console.log('加载预制体失败');
                    reject(error)
                } else {
                    for (let i = 0; i < data.length; i++) {
                        console.log(data[i].name);
                        
                        const element = data[i] as Prefab;
                        if(element.name == PlantTypeMap[plantType]){
                            resolve(element)
                            return;
                        }
                    }
                }
            })
        })
    }
    placePlant(cell:Cell){
        if(this.currentPlant == null) return;
        const isSuccess = cell.addPlant(this.currentPlant)
        if (isSuccess){
            this.currentPlant.getComponent(Plant).transitionEnable()
            // 释放当前的植物
            this.currentPlant = null
        } 
    }
}

 